<?php

namespace Apps\AppApi\Method\Activity;

use Apps\AppApi\Method\BaseMethod;
use Apps\Common\Libs\PicUrl;
use Apps\Common\Models\Activity;
use Apps\Common\Models\ActivityBeautyParlor;
use PhalconPlus\Api\Common\ResultStatus;
use Apps\Common\Models\ActivityViewsUser;


/**
 * 活动详情
 * @package Apps\AppApi\Method\Activity
 */
class GetInfo extends BaseMethod
{
    public function run()
    {
        $id = intval($this->getDataItem('id', 0));
        $user_id = intval($this->getDataItem('user_id', 0));

        if ($id == 0) {
            return $this->errorLog(ResultStatus::URL_PARAM_CANNOT_EMPTY, 'URL参数不全');
        }

        $Activity = new Activity();
        $ActivityViewsUser = new ActivityViewsUser();
        $data = $Activity->query()->columns(['activity_id id',
            'activity_type type',
            'activity_title as title',
            'activity_cover as cover',
            'activity_start_time',
            'activity_end_time',
            'activity_intro',
            'out_link',
            'views',
            'comments',
        ])->where("activity_id = {$id}")->execute()->toArray();
        $data = $data ? $data[0] : [];
        $data['user_views_list'] = $ActivityViewsUser->getViewsUserList($id, 0, 7);
        $data['cover'] = $data['cover'] ? PicUrl::ActivityCover($data['cover'], $this->getDi()) : '';
        $data['out_link'] = $data['out_link'] ?: '';

        $this->setResult($data);

        $count = 1;
        $Activity->getReadConnection()->query("UPDATE activity SET views = views + {$count} WHERE activity_id = {$id}");
        if ($user_id) {
            $ActivityViewsUser->addData($id, $user_id);
        }

        /*if(0 < $id) {
            $this->activity = Activity::query()
                ->columns(['activity_id id',
                    'city_id city',
                    'activity_type type',
                    'activity_title as title',
                    'activity_intro as intro',
                    '0 beauty_parlors',
                ])
                ->where('activity_id=:id:')
                ->bind(['id'=>$id])
                ->execute()->getFirst();
            if($this->activity){
                $parlor = ActivityBeautyParlor::query()
                    ->columns([
                        'bp_id as id',
                        'bp_name as name',
                        'bp_cover as cover',
                        'bp_address as address',
                        '0 as tags',
                        'bp_medal as medal'])
                    ->leftJoin('Apps\Common\Models\BeautyParlor',
                        'Apps\Common\Models\ActivityBeautyParlor.beauty_parlor_id=bp.bp_id','bp')
                    ->where('bp.bp_state > 0 and activity_id = :id:')
                    ->bind(['id' => $id])
                    ->execute()->toArray();
                $this->activity->beauty_parlors = $parlor;
            }
        }

        $this->activity = $this->activity ? $this->activity : null;
        if($this->activity) {
            //$this->activity->cover = PicUrl::ActivityCover($this->activity->cover, $this->getDi());
            for($i = 0; $i < count($this->activity->beauty_parlors); $i++) {
                $this->activity->beauty_parlors[$i]['cover'] = PicUrl::BeautyParlorCover($this->activity->beauty_parlors[$i]['cover'], $this->getDi());

                $medal = $this->activity->beauty_parlors[$i]['medal'];
                unset($this->activity->beauty_parlors[$i]['medal']);
                $this->activity->beauty_parlors[$i]['medal_refund'] = \Apps\Common\Libs\BeautyParlor::getMedalRefund($medal);
                $this->activity->beauty_parlors[$i]['medal_verify'] = \Apps\Common\Libs\BeautyParlor::getMedalVerify($medal);
                $this->activity->beauty_parlors[$i]['tags'] = \Apps\Common\Libs\BeautyParlor::getTags($this->activity->beauty_parlors[$i]['id']);
            }

            $this->activity->intro = \Apps\Common\Libs\BeautyParlor::formatServiceIntro($this->activity->intro, $this->getDi(), true);
        }
        $this->setResult($this->activity);*/
    }

}